from appium import webdriver
from  selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
import os,time
class Base():
    def __init__(self):
        os.system('adb root')
        cap = {
            "platformName": "Android",
            "deviceName": "emulator-5554",
            "appPackage": "com.kongfz.app",
            "appActivity": "com.kongfz.app.home.mvvm.homepage.HomeActivity",
            "noReset": True
        }
        self.driver = webdriver.Remote('http//127.0.0.1:4327/wd/hub',cap)
        self.driver.implicitly_wait(25)
        sleep(25)

    def find_ele(self,loc):
        try:
            if type(loc) ==str:
                return self.driver.find_element_by_android_uiautomator(loc)
            else:
                return self.driver.find_element(*loc)
        except Exception as e:
            print("报错信息",e)
            print("未定位到页面元素的变量值",loc)

    def find_wait_ele(self, loc):
        try:
                return WebDriverWait(self.driver,15).until(EC.presence_of_element_located(loc))
        except Exception as e:
            print("报错信息", e)
            print("未定位到页面元素的变量值", loc)
    def jieping(self,filename):
        t = time.strftime('%Y%m%d%H%M%S',time.localtime())
        self.driver.save_screenshot(t+filename)
    def get_key_value(self,loc,key):
        ele = self.find_ele(loc)
        return ele.get_attribute(key)
    def qingqiao(self,x,y):
        self.driver.tap([(x,y)],200)
    def huadong(self,x,y,x1,y1):
        self.driver.swipe(x,y,x1,y1)
    def quit_driver(self):
        self.driver.quit()